(function(){
    $.extend($.fn,{
        mask: function(msg,maskDivClass){
            this.unmask();
            // 参数
            var op = {
                opacity: 0.7,
                z: 10000,
                bgcolor: '#303030'
            };
            var original=$(document.body);
            var position={top:0,left:0};
            if(this[0] && this[0]!==window.document){
                original=this;
                position=original.position();
            }
            // 创建一个 Mask 层，追加到对象中
            var maskDiv=$('<div class="maskdivgen">&nbsp;</div>');
            maskDiv.appendTo(original);
            var maskWidth=original.outerWidth();
            if(!maskWidth){
                maskWidth=original.width();
            }
            var maskHeight=original.outerHeight();
            if(!maskHeight){
                maskHeight=original.height();
            }
            maskDiv.css({
                position: 'absolute',
                top: position.top,
                left: position.left,
                'z-index': op.z,
                width: maskWidth,
                height:maskHeight,
                'background-color': op.bgcolor,
                opacity: 0
            });
            if(maskDivClass){
                maskDiv.addClass(maskDivClass);
            }
            if(msg){
                var msgDiv=$('<div style="width: 200px;height:20px; z-index: 20000; position: absolute; text-align: center; left: 50%; top: 50%;">' +
                    '<div class="progress progress-striped active" style="margin-bottom: 0;"><div class="progress-bar" style="width: 100%;"></div></div>'+msg+'</div></div>');
                msgDiv.appendTo(maskDiv);
                var widthspace=(maskDiv.width()-msgDiv.width());
                var heightspace=(maskDiv.height()-msgDiv.height());
                msgDiv.css({
                    cursor:'wait',
                    top:(heightspace/2-2),
                    left:(widthspace/2-2)
                });
            }
            maskDiv.fadeIn('fast', function(){
                // 淡入淡出效果
                $(this).fadeTo('fast', op.opacity);
            })
            return maskDiv;
        },
        unmask: function(){
            var original=$(document.body);
            if(this[0] && this[0]!==window.document){
                original=$(this[0]);
            }
            original.find("> div.maskdivgen").fadeOut('slow',0,function(){
                $(this).remove();
            });
        }
    });
})();
/**
 * 显示loading效果，提出该类是为了统一loading效果
 * @constructor
 */
var Loading = function () {

    var defaultMsg = "正在加载...";

    var show = function (msg,id) {
        if(id){
            $(id).mask(msg || defaultMsg);
        }else{
            $(document).mask(defaultMsg);
        }
    }

    var hide = function (id) {
        if(id){
            $(id).unmask();
        }else{
            $(document).unmask();
        }
    }
    return {
        show: show,
        hide: hide
    }
}();

(function ($) {
    /* plugin goes here */
    $.fn.loading = function (msg) {
        Loading.show(msg, this);
    };
    $.fn.loaded = function () {
        Loading.hide(this);
    }
})(jQuery);
